package com.boxin.ims.modules.wechat.busi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.dao.DaoHelper;
import com.boxin.ims.modules.wechat.entity.ImageMessage;
import com.boxin.ims.modules.wechat.entity.NewsReply;
import com.boxin.ims.modules.wechat.entity.TextMessage;
import com.boxin.ims.modules.wechat.entity.WechatConfig;
import com.boxin.ims.modules.wechat.service.WechatConfigService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;

/**
 * @author Jakemanse
 * @date 2013-10-3
 * @function <p> </p>
 */
@Service
public class DefaultProcessor extends BusinessProcessor {
	
	@Autowired
	DaoHelper daoHelper;
	
	@Autowired
	WechatConfigService wechatConfigService;
	
	public DefaultProcessor() {
	}

	@Override
	public ProcessMessage process() {
		String reply = weChat.getRemarks();		//默认回复内容
		WechatConfig cfg = 	WechatUtils.getConfigByUpContent(defaultReplyList(), textMessage.getContent());
		if(cfg != null){//找到对应的交互答案
			if(ImageMessage.MSG_TYPE_NEWS.equals(cfg.getMsgType())){
				ImageMessage imageMessage = new ImageMessage(getMessageMap());
				Map<String,String> params = new HashMap<String,String>();
				params.put("configId", ""+cfg.getId());
				List<NewsReply> newsReplyList = daoHelper.queryForList("com.boxin.ims.wechat.selectNewsReplys", params);	//newsReplyService.findNewsReply(params);
				
				replaceUrlParameters(newsReplyList);
				
				String xml = imageMessage.replyMessage(newsReplyList);
				writeImageXMLToUser(xml);
				return null;
			}else if(TextMessage.MSG_TYPE.equals(cfg.getMsgType())){
				reply = cfg.getDownContent();
				
			}
		} 
		
		if(StringUtils.isNotEmpty(reply)){
			reply = replaceUrlParameters(reply);
		}
		
		writeToUser(reply);
		printReplyLogger(reply);
		
		
		return null;
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-3  上午8:59:00
	 * @function <p> 用户自定义回复列表 </p>
	 * @return
	 */
	public List<WechatConfig> defaultReplyList(){
		WechatConfig wechatConfig = new WechatConfig();
		wechatConfig.setWeChat(weChat);
		List<WechatConfig> configList = wechatConfigService.find( wechatConfig);	//关键词列表
		return configList;
	}

	@Override
	public boolean isNeedBeMember() {
		// TODO Auto-generated method stub
		return false;
	}

}
